package com.ae.shield.common.items.shieldFittings.makers;

import com.ae.shield.common.enchantment.shieldSuffix.SuffixList;
import com.ae.shield.common.items.util.ItemNBTHelper;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.EffectInstance;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.energy.IEnergyStorage;

import javax.annotation.Nonnull;

import java.util.List;

import static net.minecraft.potion.Effects.INSTANT_HEALTH;
import static net.minecraft.potion.Effects.SPEED;

public class HEV extends ShieldMakerBase{
    private final String TAG_CURE= "cure";

    public HEV() {
        super(new Properties(), new ShieldProperty()
                .recovery(0.125f)
                .cd(48)
                .maxIntensity(32f)
                .close(1.2f)
                .remote(1.2f)
                .magic(1.6f)
                .other(0.2f)
                .upperLimit(30f)
                .floorLimit(0f)
                .maxPiles(2)
                .penalty(10f)
                .cost(10));
    }

    @Override
    public void addCtrlInformation(List<ITextComponent> components, ItemStack stack) {
        components.add(new TranslationTextComponent("tooltip.ae.shield.hev.tip1").mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.hev.tip2").mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.hev.tip3").mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.hev.tip4").mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.hev.tip5").mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.hev.tip6").mergeStyle(TextFormatting.GRAY));
    }

    @Override
    public void shieldTick(@Nonnull ItemStack stack, @Nonnull World world, @Nonnull Entity entity, IEnergyStorage energyCap) {
        if (!(entity instanceof LivingEntity)) {
            return;
        }

        cureEntity(stack, (LivingEntity) entity, energyCap);
    }

    private void cureEntity(ItemStack stack, LivingEntity entity, IEnergyStorage energyCap) {
        int cure = ItemNBTHelper.getInt(stack, TAG_CURE, 0);

        if (cure < 1){
            int cost = energyCap.extractEnergy(50, true);
            if (cost >= 50 && entity.getHealth() < entity.getMaxHealth()) {
                entity.addPotionEffect(new EffectInstance(INSTANT_HEALTH, 1, 0, false, false, false));
                entity.addPotionEffect(new EffectInstance(SPEED, 25, 1, false, false, false));
                int level = EnchantmentHelper.getEnchantmentLevel(SuffixList.LEVEL_UP.get(), stack);
                ItemNBTHelper.setInt(stack, TAG_CURE, 20 - level * 2);
                energyCap.extractEnergy(50 + level * 10, false);
            }
        }else ItemNBTHelper.setInt(stack, TAG_CURE,cure - 1);
    }

    @Override
    public void onCreated(@Nonnull ItemStack stack, @Nonnull World worldIn, @Nonnull PlayerEntity playerIn) {
        super.onCreated(stack, worldIn, playerIn);
        ItemNBTHelper.setInt(stack, TAG_CURE, 0);
    }
}
